﻿using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Gp.Scripts.Editor
{
    public class SettleEditor : OdinMenuEditorWindow
    {
        [MenuItem("常用工具/关卡编辑器")]
        private static void ShowWindow()
        {
            var window = GetWindow<SettleEditor>();
            window.titleContent = new GUIContent("关卡编辑器");
            window.Show();
        }


        protected override OdinMenuTree BuildMenuTree()
        {
            var tree = new OdinMenuTree
            {
                DefaultMenuStyle = { IconSize = 30.00f },
                Config = { DrawSearchToolbar = true }
            };

            tree.Add("关卡操作", new SettleMainNode(this));
            tree.Add("相机视角", new CameraPivotNode(this));
            tree.Add("战斗模拟器", new CameraPivotNode(this));

            return tree;
        }


        protected override void OnDestroy()
        {
            // 注销事件
            EditorSceneManager.sceneClosed -= OnSceneChanged;
            EditorSceneManager.sceneOpened -= OnSceneChanged;
        }


        private void Awake()
        {
            // 注册场景转换事件，当发生场景转换时刷新
            EditorSceneManager.sceneClosed += OnSceneChanged;
            EditorSceneManager.sceneOpened += OnSceneChanged;
        }

        private void OnSceneChanged(Scene scene, OpenSceneMode mode)
        {
            OnSceneChanged(scene);
        }


        private void OnSceneChanged(Scene scene)
        {
            ForceMenuTreeRebuild();
        }
        
        protected override void OnBeginDrawEditors()
        {
            // var toolbarHeight = MenuTree.Config.SearchToolbarHeight;
            // // 绘制工具栏
            // SirenixEditorGUI.BeginHorizontalToolbar(toolbarHeight);
            // {
            //     if (SirenixEditorGUI.ToolbarButton(new GUIContent("刷新窗口")))
            //     { ForceMenuTreeRebuild(); }
            // }
            // SirenixEditorGUI.EndHorizontalToolbar();
        }
    }
}